package it.fuesi.synoutils.services.tasks.info;

import android.content.Context;
import android.util.Log;

import it.fuesi.synoutils.activities.MainActivity;
import it.fuesi.synoutils.fragments.HomeFragment;
import it.fuesi.synoutils.model.info.DSMInfo;
import it.fuesi.synoutils.model.info.DSMInfoClientParameters;
import it.fuesi.synoutils.model.info.DSMInfoResponse;
import it.fuesi.synoutils.model.info.VolumesListResponse;
import it.fuesi.synoutils.services.client.InfoClient;
import it.fuesi.synoutils.services.client.ServiceClientFactory;
import it.fuesi.synoutils.services.tasks.base.BaseTask;

/**
 * Created by pascotto on 17/10/14.
 */
public class DSMInfoTask extends BaseTask<DSMInfoResponse, DSMInfo, DSMInfoClientParameters> {

    private static final String TAG = DSMInfoTask.class.getName();

    public DSMInfoTask(Context context){
        super(context);
    }

    private static final String CACHE_KEY = "dsminfo";


    @Override
    protected String getCacheKey() {
        return CACHE_KEY;
    }

    @Override
    public DSMInfoResponse run(DSMInfoClientParameters params)  throws Exception{
            boolean expireCache = params.refreshCache();

            InfoClient client = ServiceClientFactory.getClient(InfoClient.class);

            DSMInfoResponse resp;

            Log.d(TAG, "Fetching data");
            publishProgress("Loading info from disk station");
            resp = client.info();

            publishProgress("Loading volumes from disk station");
            final VolumesListResponse volumes = client.getVolumes();
            if(volumes.isSuccess())
                resp.getData().setVolumes(volumes.getData().getVolumes());


            return resp;

    }


    @Override
    protected void onPostExecute(DSMInfoResponse dsmInfoResponse) {
        super.onPostExecute(dsmInfoResponse);

        if(dsmInfoResponse != null){
            final MainActivity act = (MainActivity)getContext();
            final HomeFragment home = (HomeFragment)act.getCurrentFragment();
            home.setInfo(dsmInfoResponse.getData());
        }



    }
}
